import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

class SpUtil {
  static String _prefix = '';

  /// 初始化
  static init(String prefix) async {
    _prefix = '$prefix.';
  }

  /// set
  static setString(String key, String value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setString('$_prefix$key', value);
  }

  static setInt(String key, int value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setInt('$_prefix$key', value);
  }

  static setDouble(String key, double value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setDouble('$_prefix$key', value);
  }

  static setBool(String key, bool value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setBool('$_prefix$key', value);
  }

  static setMap(String key, Map<dynamic, dynamic> value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setString('$_prefix$key', jsonEncode(value));
  }

  static setList(String key, List<dynamic> value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setString('$_prefix$key', jsonEncode(value));
  }

  /// get
  static Future<String?> getString(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getString('$_prefix$key');
  }

  static Future<int?> getInt(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getInt('$_prefix$key');
  }

  static Future<double?> getDouble(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getDouble('$_prefix$key');
  }

  static Future<bool?> getBool(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getBool('$_prefix$key');
  }

  static Future<Map<dynamic, dynamic>?> getMap(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    String? valueS = sp.getString('$_prefix$key');
    return valueS == null ? valueS : jsonDecode(valueS);
  }

  static Future<List<dynamic>?> getList(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    String? valueS = sp.getString('$_prefix$key');
    return valueS == null ? valueS : jsonDecode(valueS);
  }

  ///del
  static delKey(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.remove('$_prefix$key');
  }

  static delAll() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.clear();
  }

  static Future<Set<String>> getKeys() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getKeys();
  }

  static Future<String?> getAppId() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getString('APP_ID');
  }

  static setAppId(String value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setString('APP_ID', value);
  }

  static delAppId() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.remove('APP_ID');
  }
}
